package middleware

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func AuthMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		token := ctx.GetHeader("Authorization")
		if token == "" {
			logrus.Warn("Authorization header missing")
			ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
			return
		}

		// 这里可以添加更复杂的token验证逻辑
		if token != "Bearer valid-token" {
			logrus.Warn("Invalid token")
			ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
			return
		}

		ctx.Next()
	}
}

func LoggingMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		logrus.WithFields(logrus.Fields{
			"method": ctx.Request.Method,
			"path":   ctx.Request.URL.Path,
		}).Info("Incoming request")

		ctx.Next()

		logrus.WithFields(logrus.Fields{
			"status": ctx.Writer.Status(),
		}).Info("Request completed")
	}
}
